package com.compmov.socialnetwork;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;

import com.compmov.datamodel.Foto;
import com.compmov.libraries.ImageLoader;
import com.compmov.webservice.FotoWSClient;
import com.compmov.webservice.WSClientException;


public class Photos extends Activity {
	 private Gallery gallery;
	 public ImageLoader imageLoader;
	 private Activity activity=this;
	 private List<Foto> fotos;
	 private String id;
	 private static LayoutInflater inflater = null;


	    private ImageView imgView;




	    @Override

	    public void onCreate(Bundle savedInstanceState) {

	        super.onCreate(savedInstanceState);

	        setContentView(R.layout.photos);
	        
	        Bundle extras = getIntent().getExtras();
	        id = extras.getString("id_lugar");
	        
	       
	        FotoWSClient fotoWS = new FotoWSClient("fotos");
			try {
				 fotos = fotoWS.getFotos(Integer.parseInt(id));
				 if(fotos.isEmpty()){
					 return;
				 }
				 
			} catch (WSClientException e) {
				// TODO Auto-generated catch block
				Toast.makeText(getBaseContext(),e.getMessage(),
						Toast.LENGTH_SHORT).show();
				return;
			}
	        
	        
			inflater = (LayoutInflater) activity
			.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			imageLoader = new ImageLoader(this.getApplicationContext());


	        imgView = (ImageView)findViewById(R.id.ImageMostrar);
	        
	        //imageLoader.DisplayImage(fotos.get(0).getUrl(), activity, imgView);
	        Drawable image = ImageOperations(activity,fotos.get(0).getUrl(),"image"+0+".jpg");
    		imgView.setImageDrawable(image);
        
	         gallery = (Gallery) findViewById(R.id.examplegallery);
	         gallery.setAdapter(new AddImgAdp(this));
	         gallery.setOnItemClickListener(new OnItemClickListener() {

	            public void onItemClick(AdapterView parent, View v, int position, long id) {

	            	Foto fot=fotos.get(position);
	            	//imageLoader.DisplayImage(fot.getUrl(), activity, imgView);
	            	Drawable image = ImageOperations(activity,fot.getUrl(),"image"+position+".jpg");
	 	    		imgView.setImageDrawable(image);
	                //imgView.setImageResource(Imgid[position]);*/

	            }

	        });
	    }


	    private class AddImgAdp extends BaseAdapter {

	        int GalItemBg;
	        private Context cont;
	        public AddImgAdp(Context c) {
	            cont = c;
	            TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
	            GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
	            typArray.recycle();
	        }


	        public int getCount() {
	            return fotos.size();
	        }

	        public Object getItem(int position) {
	            return position;
	        }

	        public long getItemId(int position) {
	            return position;
	        }

	        public View getView(int position, View convertView, ViewGroup parent) {
	            ImageView imgView1 = new ImageView(cont);
	            
	            Foto fot=fotos.get(position);
	            imgView1.setLayoutParams(new Gallery.LayoutParams(80, 70));
	            imgView1.setScaleType(ImageView.ScaleType.FIT_XY);
	            imgView1.setBackgroundResource(GalItemBg);
	            
	            //imageLoader.DisplayImage(fot.getUrl(), activity, imgView1);
            	
	            Drawable image = ImageOperations(activity,fot.getUrl(),"imagetemp"+position+".jpg");
	    		imgView1.setImageDrawable(image);
	            
	            return imgView1;
	        }

	    }
	
		@Override
		public boolean onCreateOptionsMenu(Menu menu) {
			MenuInflater inflater = getMenuInflater();
			inflater.inflate(R.menu.menu_images, menu);
			return true;
		}

		//  @Override
		public boolean onOptionsItemSelected(MenuItem item) {
			Intent intent;
			switch (item.getItemId()) {
			case R.id.itemAdd:
				 intent = new Intent(this.getApplicationContext(),AddImages.class);
				 intent.putExtra("id_lugar", id);
				 startActivity(intent);   
				return true;
			default:
				return super.onOptionsItemSelected(item);
			}
		}
		
		private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
			try {
				InputStream is = (InputStream) this.fetch(url);
				Drawable d = Drawable.createFromStream(is, "src");
				return d;
			} catch (MalformedURLException e) {
				e.printStackTrace();
				return null;
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			}
		}

		public Object fetch(String address) throws MalformedURLException,IOException {
			URL url = new URL(address);
			Object content = url.getContent();
			return content;
		}

}
